import Koa from 'koa2';
import cors from 'koa2-cors';
import {koaBody} from 'koa-body';
import koaJwt from 'koa-jwt';
import router from './router/index.js';
import {handleQueue} from './utils/messageQueue.js'
import mongoConnect from './db/index.js'
mongoConnect()
const app = new Koa();
const port = 12355;

app.use(async (ctx, next) => {
    try {
        await next();
        if (parseInt(ctx.status) === 404) {
            ctx.body = '';
        }
    } catch (err) {
        if (parseInt(err.status) === 401) {
            ctx.status = 200;
            ctx.body = 'expired';
        } else {
            console.log(err);
            // throw err;
        }
    }
});

app.use(cors());
app.use(koaBody());
// app.use(koaJwt({ secret: 'duckDuckers' }).unless({ path: [/^\/api\/login/] }));
app.use(router.routes(), router.allowedMethods());

app.listen(port, () => {
    console.log('Server is running at http://localhost:' + port);
});
handleQueue();
